package com.baizhi.cmfz.controller;

import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.entity.Article;
import com.baizhi.cmfz.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * This is a Class.
 *
 * @author zhanglh
 * @date 2019/09/11
 */
@RestController
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("all")
    @LogAnnotation(value = "查找所有文章")
    Map selectAllArticle(int page, int limit, String articleName) {
        return articleService.selectAllArticle(page, limit, articleName);
    }

    @RequestMapping("add")
    @LogAnnotation(value = "添加文章", type = "insert")
    public boolean addArticle(Article article, HttpServletRequest request, MultipartFile file) {
        if (file != null) {
            //设置文件名
            String oldFileName = file.getOriginalFilename();
            String fileName = "/img/" + new Date().getTime() + "_" + oldFileName;
            //上传路径
            String path = request.getServletContext().getRealPath("/");
            //用流复制
            try {
                file.transferTo(new File(path + fileName));
                article.setArticleImage(fileName);
                return articleService.addArticle(article) > 0;
            } catch (IOException e) {
                return false;
            }
        } else {
            return articleService.addArticle(article) > 0;
        }
    }

    @RequestMapping("remove")
    @LogAnnotation(value = "删除文章", type = "delete")
    public boolean removeArticle(int[] ids) {
        return articleService.removeArticle(ids) > 0;
    }

    @RequestMapping("update")
    @LogAnnotation(value = "更新文章信息", type = "update")
    public boolean updateArticle(Article article, HttpServletRequest request, MultipartFile file) {
        if (file != null) {
            //设置文件名
            String oldFileName = file.getOriginalFilename();
            String fileName = "/img/" + new Date().getTime() + "_" + oldFileName;
            //上传路径
            String path = request.getServletContext().getRealPath("/");
            //用流复制
            try {
                file.transferTo(new File(path + fileName));
                article.setArticleImage(fileName);
                return articleService.updateArticle(article) > 0;
            } catch (IOException e) {
                return false;
            }
        } else {
            return articleService.updateArticle(article) > 0;
        }
    }

    //冻结上师文章
    @RequestMapping("freeze")
    @LogAnnotation(value = "改变文章状态", type = "update")
    public boolean freeze(Article article) {
        return articleService.updateArticle(article) > 0;
    }

    @RequestMapping("selectOne")
    @LogAnnotation(value = "根据id查找文章")
    Article selectOneArticle(int articleId) {
        return articleService.selectOneArticle(articleId);
    }
}
